//
//  PressThisButtonViewController.m
//  PressThisButton
//
//  Created by Brennan Stehling on 10/5/09.
//  Copyright Offwhite 2009. All rights reserved.
//

#import "PressThisButtonViewController.h"
#import "CJSONDeserializer.h"

@implementation PressThisButtonViewController

#pragma mark -
#pragma mark Properties

@synthesize staticButton;
@synthesize remoteButton;
@synthesize doNothingButton;
@synthesize label;

#pragma mark -
#pragma mark Button Methods

- (IBAction) pressStaticButton {
	NSLog(@"pressStaticButton");
	[self parseStaticString];
}

- (IBAction) pressRemoteButton {
	NSLog(@"pressRemoteButton");
	[self parseRemoteString];
}

- (IBAction) pressNothingButton {
	NSDictionary *dictionary = [[NSDictionary alloc] init];
	//[dictionary setValue:@"Nothing" forKey:@"labelText"];
	[self updateLabelText:dictionary];
	
	// CAG: needed because dictionary is alloced here.
	[dictionary release];
}

#pragma mark -
#pragma mark Worker Methods

- (void)parseStaticString {
	NSString *jsonString = @"{ \"labelText\" : \"Static test is successful!\" }";	
	NSDictionary *dictionary = [self parseString:jsonString];
	[self updateLabelText:dictionary];
}

- (void)parseRemoteString {
	NSString *urlString = @"http://www.theresgonnabesnacks.com/data/basic.json";
	
	// Create NSURL string from formatted string
	NSURL *url = [NSURL URLWithString:urlString];
	
	// Setup and start async download
	NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
	NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
	
	[connection release];
	[request release];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

	// Store incoming data into a string
	NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	
	NSLog(@"jsonString = \"%@\"", jsonString);

	// Create a dictionary from the JSON string
	NSDictionary *dictionary = [self parseString:jsonString];
	
	[self updateLabelText:dictionary];
	
	[jsonString release];
}

- (void)updateLabelText:(NSDictionary *) dictionary {
	bool hasKey = [[dictionary allKeys] containsObject:@"labelText"];
	
	if (hasKey) {
		NSString *labelText = [dictionary objectForKey:@"labelText"];
		NSLog(@"%@", labelText);
		label.text = labelText;
	}
	else {
		NSLog(@"No value to use for label text.");
	}
	
}

- (NSDictionary *)parseString:(NSString *) jsonString {
	NSLog(@"jsonString = \"%@\"", jsonString);
	
	// Use TouchJSON to populate the data and dictionary
	NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
	NSError *error = nil;
	NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
	
	if (error) {
		NSLog(@"An error occurred while parsing the JSON string: %@", jsonString);
	}

	return dictionary;
}

#pragma mark -
#pragma mark Memory Management

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)dealloc {
	[staticButton release];
	[remoteButton release];
	[doNothingButton release];
	[label release];
	
    [super dealloc];
}

@end
